<script lang="ts">
  import { AlertType } from "./conf"
  import {closeAlert} from "@store/global";
  export let message = ""
  export let type: AlertType;
  export let idx: number =  -1;

  let alertBoxClass: string;
  let alertTextColorClass: string;
  let alertBtnClass: string;
  
  switch (type) {
    case AlertType.Error:
      alertBoxClass= "alert-error-box";
      alertTextColorClass= "alert-error-text-color";
      alertBtnClass= "alert-error-btn";
      break;
    default:
      alertBoxClass= "alert-info-box";
      alertTextColorClass= "alert-info-text-color";
      alertBtnClass= "alert-info-btn";
  }

  function handleClose () {
    closeAlert(idx);
  }

</script>
<div class="flex p-4 mb-4 border-t-4 {alertBoxClass}" role="alert">
  <svg class="flex-shrink-0 w-5 h-5 {alertTextColorClass}" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"></path></svg>
  <div class="ml-3 text-sm font-medium {alertTextColorClass}">
    {message}
  </div>
  <button on:click={handleClose} type="button" class="ml-auto -mx-1.5 -my-1.5  rounded-lg focus:ring-2 p-1.5 inline-flex h-8 w-8 {alertBtnClass}" data-dismiss-target="#alert-border-5" aria-label="Close">
    <span class="sr-only">Dismiss</span>
    <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
  </button>
</div>
<style lang="postcss">
  /** alert **/
  .alert-info-box {
    @apply border-gray-500 bg-gray-700;
  }
  .alert-info-btn {
    @apply bg-gray-700 text-gray-300;
  }
  .alert-info-btn:focus {
    @apply ring-gray-400;
  }
  .alert-info-btn:hover {
    @apply bg-gray-800 text-white; 
  }
  .alert-info-text-color {
    @apply text-gray-300;
  }
  .alert-error-box {
    @apply border-red-500 bg-red-200;
  }
  .alert-error-btn {
    @apply bg-red-200 text-red-500;
  }
  .alert-error-btn:focus {
    @apply ring-red-400;
  }
  .alert-info-btn:hover {
    @apply bg-red-300;
  }
  .alert-error-text-color {
    @apply text-red-700;
  }
</style>